/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : innoTweets Javascript Functions
 * 
 */

var ws, url = '';

// Initilize shadowbox
Shadowbox.init({
  skipSetup: true
});

$(document).ready(function() {

  // Setting toaster options
  toastr.options = {
	timeOut: 5000,
	positionClass: 'toast-bottom-right'
  };

  // Check browser html5
  if (!html5_supported()) {
	toastr.info('Please update your browser !');
  }

  // Load jquery knob
  $(".dial").knob();

  // Load jquery piety
  $(".pbar").peity("bar", {
	colours: ["#EA494A"],
	max: 10,
	min: 0
  });

  // Perform onload operations
  crawler_warning();
  glow_div();

  // Perform oload operations if signed id
  if (is_signin()) {
	notification();
	incomplete_profile();
  }
});

$(window).on('hashchange', function() {
  glow_div();
});

function glow_div() {
  var div_id = window.location.hash.substr(1);
  var id_val = $.trim(div_id.replace(/[^0-9]/g, ''));
  if (div_id && id_val) {
	$('#' + div_id).bind("fadeglow", function() {
	  $(this).fadeOut(1000).fadeIn(1000, function() {
		$(this).fadeOut(1000).fadeIn(1000, function() {
		  $(this).attr('style', 'text-decoration: none;');
		});
	  }).attr('style', 'text-decoration: none;');
	}).trigger("fadeglow");
  }
}

function expand_group() {
  var div_id = window.location.hash.substr(1);
  var id_val = $.trim(div_id.replace(/[^0-9]/g, ''));
  if (div_id && id_val) {
	$('#group_' + id_val).addClass('in');
  }
}

function html5_supported() {
  var elem = document.createElement('canvas');
  return !!(elem.getContext && elem.getContext('2d'));
}

function site_url(url) {
  url = (url) ? url : '';
  var site_url = $.trim($('#site_url').val());
  return site_url + url;
}

function is_iframe() {
  if (window.self === window.top) {
	return false;
  } else {
	return true;
  }
}

function is_signin() {
  var is_signin = $.trim($('#is_signin').val());
  if (is_signin == 1) {
	return true;
  } else {
	return false;
  }
}

function incomplete_profile() {
  var incomplete_profile = $.trim($('#incomplete_profile').val());
  if (incomplete_profile == 1) {
	toastr.warning('Please complete your profile !');
  }
}

function crawler_warning() {
  var crawler_warning = $.trim($('#crawler_warning').val());
  if (crawler_warning == 1) {
	toastr.warning('Slow down posting. Wait for 10sec');
	return false;
  }
}

function post_tweet_reply(tweet_id) {
  if ((tweet_id) && (validate_reply())) {
	var tweet_reply = $.trim($('#tweet_reply').val());
	$('#post_reply_button').attr('disabled', 'disabled');
	$('#loading_anim_blue').show();
	$.ajax({
	  type: 'POST',
	  cache: false,
	  url: site_url('reply/post/' + tweet_id),
	  complete: function() {
		$('#post_reply_button').removeAttr('disabled');
	  },
	  data: {
		tweet_reply: tweet_reply
	  },
	  success: function(data) {
		var json = $.parseJSON(data);
		if (json.status == 1) {
		  send_notification('new_reply', tweet_id, json.data.hash);
		}
		load_new_replies(false);
		follow_tweet(tweet_id, 1);
		$('#tweet_reply').data("wysihtml5").editor.setValue('');
	  }
	});
  }
}

function delete_tweet_reply(tweet_reply_id) {
  if (tweet_reply_id) {
	bootbox.confirm("Delete tweet reply ?", function(result) {
	  if (result) {
		$.ajax({
		  type: 'POST',
		  cache: false,
		  url: site_url('reply/delete/' + tweet_reply_id),
		  success: function(data) {
			load_new_replies(false);
		  }
		});
	  }
	});
  }
}

// Comment Action

function post_comment(comment_type, comment_type_id) {
  if ((comment_type) && (comment_type_id) && (validate_comment())) {
	var comment_message = $.trim($('#comment_message').val());
	$('#post_comment_button').attr('disabled', 'disabled');
	$('#loading_anim_blue').show();
	$.ajax({
	  type: 'POST',
	  cache: false,
	  url: site_url('comment/post'),
	  complete: function() {
		$('#post_comment_button').removeAttr('disabled');
	  },
	  data: {
		comment_type: comment_type,
		comment_type_id: comment_type_id,
		comment_message: comment_message
	  },
	  success: function(data) {
		load_new_comments(false);
		$('#comment_message').data("wysihtml5").editor.setValue('');
	  }
	});
  }
}

function delete_comment(comment_id) {
  if (comment_id) {
	bootbox.confirm("Delete comment ?", function(result) {
	  if (result) {
		$.ajax({
		  type: 'POST',
		  cache: false,
		  url: site_url('comment/delete/' + comment_id),
		  success: function(data) {
			load_new_comments(false);
		  }
		});
	  }
	});
  }
}

// All in one like activity

function do_like_action(like_type, like_type_id, like_action) {
  $.ajax({
	type: 'POST',
	cache: false,
	url: site_url('like'),
	data: {
	  like_type: like_type,
	  like_type_id: like_type_id,
	  like_action: like_action
	},
	success: function(data) {
	  if ($.trim(data) != '') {
		var json = $.parseJSON(data);
		if ((json.count)) {
		  change_like_htmls(like_type, like_type_id, json.my_option, json.count);
		}
	  }
	}
  });
}

function change_like_htmls(like_type, like_type_id, like_action, like_count) {
  var like_count_id = '#like_count_' + like_type + '_' + like_type_id;
  var like_button_id = '#like_button_' + like_type + '_' + like_type_id;
  if (like_action == 1) {
	$(like_button_id).attr('onclick', 'return do_like_action("' + like_type + '", "' + like_type_id + '", "0")');
	$(like_button_id).html('<i class="icon-large icon-thumbs-down"></i>');
	$(like_button_id).attr('title', 'Unlike');
  } else {
	$(like_button_id).attr('onclick', 'return do_like_action("' + like_type + '", "' + like_type_id + '", "1")');
	$(like_button_id).html('<i class="icon-large icon-thumbs-up"></i>');
	$(like_button_id).attr('title', 'Like');
  }
  $(like_count_id).html(like_count + ' Likes');
}

function update_likes_count() {
  var like_buttons = new Array();
  $('.update_like_count').each(function() {
	var like_type = null;
	var like_type_id = null;
	if (this.id) {
	  like_type_id = $.trim(this.id.replace(/[^0-9]/g, ''));
	  if (like_type_id) {
		var string_parts = this.id.split("like_count_");
		if (string_parts) {
		  var string_parts_2 = string_parts[1].split("_" + like_type_id);
		  if (string_parts_2) {
			like_type = string_parts_2[0];
			if (like_type && like_type_id) {
			  var like_button = new Array(like_type, like_type_id);
			  like_buttons.push(like_button);
			}
		  }
		}
	  }
	}
  });
  $.ajax({
	type: 'POST',
	cache: false,
	url: site_url('like/update_likes_count'),
	data: {
	  like_buttons: like_buttons
	},
	success: function(data) {
	  if ($.trim(data) != '') {
		var json = $.parseJSON(data);
		if (json) {
		  $(json).each(function(i, val) {
			change_like_htmls(val.like_type, val.like_type_id, val.my_like_status, val.total_likes_count);
		  });
		}
	  }
	}
  });
  setTimeout('update_likes_count()', 60000);
}

// Load more tweets
function load_more_tweets() {
  var tweet_type = $.trim($('#tweet_type').val());
  var tweet_offset = parseInt($.trim($('#tweet_offset').val()));
  var search_term = $.trim($('#search_term').val());
  $('#loading_animation').show();
  $.ajax({
	type: 'POST',
	cache: false,
	url: site_url('tweets_load_more'),
	data: {
	  tweet_type: tweet_type,
	  tweet_offset: tweet_offset,
	  search_term: search_term
	},
	success: function(data) {
	  $('#tweet_offset').val(tweet_offset + 10);
	  $('#loading_animation').hide();
	  if ($.trim(data) != '') {
		var json = $.parseJSON(data);
		if (json.data) {
		  if (json.has_more == 0) {
			$('#load_more_buttons').fadeOut();
		  }
		  $(json.data).each(function(i, val) {
			$('#old_tweets').before(val.tweet_html);
			$('#tweet_' + val.tweet_id).slideDown('slow');
		  });
		}
	  }
	}
  });
}

// Loading new tweets
function load_new_replies(auto) {
  var last_reply_id = $.trim($('#last_reply_id').val());
  var tweet_id = $.trim($('#tweet_id').val());
  $('#loading_anim_blue').show();
  $.ajax({
	type: 'POST',
	cache: false,
	url: site_url('reply/new_ajax'),
	data: {
	  tweet_id: tweet_id,
	  last_reply_id: last_reply_id
	},
	success: function(data) {
	  $('#loading_anim_blue').hide();
	  if ($.trim(data) != '') {
		var json = $.parseJSON(data);
		if (json.new_last_reply_id) {
		  $('#last_reply_id').val(json.new_last_reply_id);
		}
		if (json.data) {
		  if (json.data != '') {
			play_notification_sound();
		  }
		  $(json.data).each(function(i, val) {
			$('#new_tweets').after(val.reply_html);
			$('#reply_' + val.reply_id).slideDown('slow');
		  });
		}
		if (json.deleted_reply_ids) {
		  $(json.deleted_reply_ids).each(function(i, val) {
			$('#reply_' + val).slideUp('slow');
		  });
		}
		if (json.total_replies_count) {
		  $('#total_replies_count_main').html(json.total_replies_count);
		  $('#total_replies_count_sub').html(json.total_replies_count);
		  $('#no_replies').hide();
		} else {
		  $('#total_replies_count_main').html(0);
		  $('#total_replies_count_sub').html(0);
		  $('#no_replies').show();
		}
		if (json.my_replies_count) {
		  $('#my_replies_count_sub').html(json.my_replies_count);
		} else {
		  $('#my_replies_count_sub').html(0);
		}
		if (json.tweet_followers_count) {
		  $('#followers_count_main').html(json.tweet_followers_count);
		} else {
		  $('#followers_count_main').html(0);
		}
		if (json.repliers_data) {
		  $('#repliers_data').html('');
		  $('#repliers_data').html(json.repliers_data);
		}
		$('#repliers_count').html(json.repliers_count);
		if (json.viewers_data) {
		  $('#viewers_data').html('');
		  $('#viewers_data').html(json.viewers_data);
		}
		$('#viewers_count').html(json.viewers_count);
	  }
	}
  });
  if (auto == true) {
	setTimeout('load_new_replies(true)', 60000);
  }
}

// Load more replies
function load_old_replies(replies_load_all) {
  var last_loaded_reply_id = $.trim($('#last_loaded_reply_id').val());
  var tweet_id = $.trim($('#tweet_id').val());
  var replies_sort_by = $.trim($('#replies_sort_by').val());
  $('#loading_animation').show();
  $.ajax({
	type: 'POST',
	cache: false,
	url: site_url('reply/load_old_replies'),
	data: {
	  tweet_id: tweet_id,
	  last_loaded_reply_id: last_loaded_reply_id,
	  replies_sort_by: replies_sort_by,
	  replies_load_all: replies_load_all
	},
	success: function(data) {
	  $('#loading_animation').hide();
	  if ($.trim(data) != '') {
		var json = $.parseJSON(data);
		if (json.new_last_loaded_reply_id) {
		  $('#last_loaded_reply_id').val(json.new_last_loaded_reply_id);
		}
		if (json.has_more == 0) {
		  $('#load_more_buttons').fadeOut();
		}
		if (json.data) {
		  $(json.data).each(function(i, val) {
			$('#old_tweet_replies').before(val.reply_html);
			$('#reply_' + val.reply_id).slideDown('slow');
		  });
		}
	  }
	}
  });
}

function load_new_comments(auto) {
  var comment_type = $.trim($('#comment_type').val());
  var comment_type_id = $.trim($('#comment_type_id').val());
  var comment_last_id = $.trim($('#comment_last_id').val());
  $('#loading_anim_blue').show();
  $.ajax({
	type: 'POST',
	cache: false,
	url: site_url('comment/load_new_comments'),
	data: {
	  comment_type: comment_type,
	  comment_type_id: comment_type_id,
	  comment_last_id: comment_last_id
	},
	success: function(data) {
	  $('#loading_anim_blue').hide();
	  if ($.trim(data) != '') {
		var json = $.parseJSON(data);
		if (json.comment_last_id_new) {
		  $('#comment_last_id').val(json.comment_last_id_new);
		}
		if (json.data) {
		  if (json.data != '' && (auto == true)) {
			$('#no_comments').fadeOut();
			play_notification_sound();
		  }
		  $(json.data).each(function(i, val) {
			$('#new_comments').after(val.comment_html);
			$('#comment_' + val.comment_id).slideDown('slow');
		  });
		}
		if (json.deleted_comment_ids) {
		  $(json.deleted_comment_ids).each(function(i, val) {
			$('#comment_' + val).slideUp('slow');
		  });
		}
		if (json.total_comments_count) {
		  $('#no_comments').fadeOut();
		  $('#total_comments_count').html(json.total_comments_count);
		} else {
		  $('#no_comments').fadeIn();
		  $('#total_comments_count').html(0);
		}
	  }
	}
  });
  if (auto == true) {
	setTimeout('load_new_comments(true)', 60000);
  }
}

function load_old_comments() {
  var comment_type = $.trim($('#comment_type').val());
  var comment_type_id = $.trim($('#comment_type_id').val());
  var comment_last_loaded_id = $.trim($('#comment_last_loaded_id').val());
  $('#loading_animation').show();
  $.ajax({
	type: 'POST',
	cache: false,
	url: site_url('comment/load_old_comments'),
	data: {
	  comment_type: comment_type,
	  comment_type_id: comment_type_id,
	  comment_last_loaded_id: comment_last_loaded_id
	},
	success: function(data) {
	  $('#loading_animation').hide();
	  if ($.trim(data) != '') {
		var json = $.parseJSON(data);
		if (json.new_last_loaded_comment_id) {
		  $('#comment_last_loaded_id').val(json.new_last_loaded_comment_id);
		}
		if (json.has_more == 0) {
		  $('#load_more').fadeOut();
		}
		if (json.data) {
		  $(json.data).each(function(i, val) {
			$('#old_comments').before(val.comment_html);
			$('#comment_' + val.comment_id).slideDown('slow');
		  });
		}
	  }
	}
  });
}

// Form validations

function validate_reply() {
  var reply_text = $.trim($('#tweet_reply').val());
  if (!reply_text) {
	toastr.error('Please enter your reply !');
	return false;
  }
  return true;
}

function validate_tweet() {
  var tweet_sub = $.trim($('#tweet_subject').val());
  var tweet_msg = $.trim($('#tweet_message').val());
  if (!tweet_sub) {
	toastr.error('Please provide tweet subject !');
	return false;
  }
  if (!tweet_msg) {
	toastr.error('Please provide tweet message !');
	return false;
  }
  return true;
}

function validate_comment() {
  var comment_msg = $.trim($('#comment_message').val());
  if (!comment_msg) {
	toastr.error('Please provide your comment !');
	return false;
  }
  return true;
}

function validate_description() {
  var desc_msg = $.trim($('#photo_description').val());
  if (!desc_msg) {
	toastr.error('Please provide description !');
	return false;
  }
  return true;
}

function validate_album() {
  var album_name = $.trim($('#album_name').val());
  if (!album_name) {
	toastr.error('Please provide album name !');
	return false;
  }
  if (album_name > 250) {
	toastr.error('Album name must be less than 250 characters !');
	return false;
  }
  return true;
}

function confirm_action(msg) {
  $('#loading_div').hide();
  var confrimAction = confirm(msg);
  if (confrimAction == false) {
	return false;
  }
  $('#loading_div').show();
  return true;
}

function validate_delete() {
  var photos_selected = $('.delete_photo_ids').is(':checked');
  if (!photos_selected) {
	toastr.error('Please select an item to delete !');
	return false;
  }
  return true;
}

function validate_event() {
  var event_date = $.trim($('#event_date').val());
  var event_message = $.trim($('#event_message').val());
  if (!event_date) {
	toastr.error('Please provide event date !');
	return false;
  }
  if (!event_message) {
	toastr.error('Please provide event message !');
	return false;
  }
  return true;
}

function validate_change_password() {
  var current_password = $.trim($('#current_password').val());
  var new_password = $.trim($('#new_password').val());
  var confirm_new_password = $.trim($('#confirm_new_password').val());
  if (!current_password) {
	toastr.error('Please provide your current password !');
	return false;
  }
  if (!new_password) {
	toastr.error('Please provide a new password !');
	return false;
  }
  if (!confirm_new_password) {
	toastr.error('Please provide confirm password !');
	return false;
  }
  if (new_password != confirm_new_password) {
	toastr.error('New password & confirm password must be same !');
	return false;
  }
  return true;
}

function validate_signup() {
  var inno_email = $.trim($('#inno_email').val());
  var inno_email_confirm = $.trim($('#inno_email_confirm').val());
  if ((!inno_email) || !is_email(inno_email)) {
	toastr.error('Please provide your innovature email id !');
	return false;
  }
  if (inno_email != inno_email_confirm) {
	toastr.error('Emails must be same.');
	return false;
  }
  return confirm_action('Send email to "' + inno_email + '" ?');
}

function is_email(email) {
  var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  return regex.test(email);
}

// Init & setup full calendar

function load_calandar(event_dates) {
  $('#calendar').fullCalendar({
	selectable: true,
	dayClick: function(date) {
	  var my_date = new Date(date).format('dd-mm-yyyy');
	  show_events(my_date);
	},
	viewDisplay: function(date) {
	  $('.fc-view').find('td').css('cursor', 'pointer');
	  var selYear = $.trim(date.title.replace(/[^0-9]/g, ''));
	  var date_json = $.parseJSON(event_dates);
	  $(date_json).each(function(i, val) {
		if (val.type == 1 || val.type == 2) {
		  var newdate = selYear + '-' + new Date(val.date).format('mm-dd');
		  var date_box_id = 'td[data-date="' + newdate + '"]';
		  $(date_box_id).css("background-color", "#CCC");
		} else {
		  var date_box_id = 'td[data-date="' + val.date + '"]';
		  $(date_box_id).css("background-color", "#CCC");
		}
	  });
	}
  });
  return;
}

function show_events(date) {
  if (date) {
	$.ajax({
	  type: 'POST',
	  cache: false,
	  url: site_url('events/show_events'),
	  data: {
		date_data: date
	  },
	  success: function(data) {
		if ($.trim(data) != '') {
		  Shadowbox.open({
			content: data,
			player: "html",
			width: 640,
			height: 480
		  });
		}
	  }
	})
  }
}

function show_liked_users(like_type, like_type_id) {
  if (like_type && like_type_id) {
	$.ajax({
	  type: 'POST',
	  cache: false,
	  url: site_url('like/liked_users'),
	  data: {
		like_type: like_type,
		like_type_id: like_type_id
	  },
	  success: function(data) {
		if ($.trim(data) != '') {
		  Shadowbox.open({
			content: data,
			player: "html",
			width: 640,
			height: 480
		  });
		}
	  }
	})
  }
  return true;
}

function add_poll_option_create() {
  var options_count = $('#poll_options_count').val();
  var new_count = parseInt(options_count) + 1;
  var new_option_html = '<div id="option_div_' + new_count + '" class="control-group"><label class="control-label"></label><div class="controls"><input name="poll_options[]" id="poll_option_' + new_count + '" type="text" style="width: 49%;" placeholder="Option #' + new_count + '" value="" maxlength="250"/></div></div>';
  $('#add_more_option').before(new_option_html);
  $('#poll_options_count').val(new_count);
}

function remove_poll_option_create() {
  var options_count = $('#poll_options_count').val();
  if (options_count == 2) {
	toastr.warning('Need atleast 2 options');
	return;
  }
  var new_count = parseInt(options_count) - 1;
  $("#option_div_" + options_count).remove();
  $('#poll_options_count').val(new_count);
}

function validate_poll() {
  var poll_msg = $.trim($('#poll_message').val());
  var poll_option_err = false;
  if (!poll_msg) {
	toastr.error('Please provide poll message !');
	return false;
  }
  $('input[name="poll_options[]"]').each(function() {
	if (!$(this).val()) {
	  poll_option_err = true;
	}
  });
  if (poll_option_err) {
	toastr.error('Please fill all options !');
	return false;
  }
  return true;
}

function validate_poll_reply() {
  var selected_option = $('.poll_option_reply').is(':checked');
  if (!selected_option) {
	toastr.error('Please select an option to vote !');
	return false;
  }
  var new_option = $('#new_poll_option').is(':checked');
  var new_option_msg = $('#new_poll_option_msg').val();
  if (new_option && $.trim(new_option_msg) == '') {
	toastr.error('Please enter your option !');
	return false;
  }
  return true;
}

function show_poll_users(poll_id, poll_option_id) {
  if (poll_id && poll_option_id) {
	$.ajax({
	  type: 'POST',
	  cache: false,
	  url: site_url('polls/show_users'),
	  data: {
		poll_id: poll_id,
		poll_option_id: poll_option_id
	  },
	  success: function(data) {
		if ($.trim(data) != '') {
		  Shadowbox.open({
			content: data,
			player: "html",
			width: 640,
			height: 480
		  });
		}
	  }
	})
  }
  return true;
}

function reply_to_user(input_field, user_name, uid) {
  if (input_field && user_name && uid) {
	var reply_user = ('<b>@' + user_name + '</b> (' + uid + ') : &nbsp;');
	$('#' + input_field).data("wysihtml5").editor.focus().setValue(reply_user);
  }
  return;
}

function set_text_value(input_field, input_text) {
  $('#' + input_field).data("wysihtml5").editor.focus().setValue(input_text);
  return;
}

// All in one like activity

function follow_tweet(tweet_id, action_type) {
  $.ajax({
	type: 'POST',
	cache: false,
	url: site_url('follow'),
	data: {
	  tweet_id: tweet_id,
	  action_type: action_type
	},
	success: function(data) {
	  if ($.trim(data) != '') {
		var json = $.parseJSON(data);
		if (json.status == 1 && (json.count)) {
		  change_follow_html(tweet_id, action_type);
		}
	  }
	}
  });
}

function change_follow_html(tweet_id, action_type) {
  if (action_type == 1) {
	$('#follow_button').attr('onclick', 'return follow_tweet("' + tweet_id + '", "0")');
	$('#follow_button').html('<i class="icon-large icon-star"></i>');
	$('#follow_button').attr('title', 'Unfollow tweet');
  } else {
	$('#follow_button').attr('onclick', 'return follow_tweet("' + tweet_id + '", "1")');
	$('#follow_button').html('<i class="icon-large icon-star-empty"></i>');
	$('#follow_button').attr('title', 'Follow tweet');
  }
}

// Send chat notification
function send_notification(notify_type, notify_type_id, notify_type_hash) {
  toastr.options.timeOut = null;
  toastr.info('Please wait while sending notifications');
  toastr.options.timeOut = 5000;
  $.ajax({
	type: 'POST',
	cache: false,
	url: site_url('send_notification/init/' + notify_type + '/' + notify_type_id + '/' + notify_type_hash),
	data: {
	  notify_type: notify_type,
	  notify_type_id: notify_type_id,
	  notify_type_hash: notify_type_hash
	},
	success: function(data) {
	  toastr.clear();
	  toastr.options.timeOut = 5000;
	  toastr.success('Notifications sent');
	}
  });
}

function write_malayalam(textbox) {
  Shadowbox.open({
	content: site_url('write_malayalam?textbox=' + textbox),
	player: "iframe",
	width: 640,
	height: 480
  });
}

function add_poll_option() {
  $('#add_option_box').show();
  $('#add_option').hide();
  $('#add_option_cancel').show();
  $('#new_poll_option').attr('checked', true);
  $("#new_poll_option_msg").focus();
}

function add_poll_option_cancel() {
  $('#add_option_box').hide();
  $('#add_option').show();
  $('#add_option_cancel').hide();
  $('#new_poll_option').attr('checked', false);
}

function load_new_options_replies() {
  var poll_id = $.trim($('#poll_id').val());
  var poll_option_last_id = $.trim($('#poll_option_last_id').val());
  $.ajax({
	type: 'POST',
	cache: false,
	url: site_url('polls/load_new_reply_and_option'),
	data: {
	  poll_id: poll_id,
	  poll_option_last_id: poll_option_last_id
	},
	success: function(data) {
	  if ($.trim(data) != '') {
		var json = $.parseJSON(data);
		if ((json.poll_option_new_last_id)) {
		  $('#poll_option_last_id').val(json.poll_option_new_last_id);
		}
		if (json.total_replies_count) {
		  $('#total_replies_count').html(json.total_replies_count);
		}
		if ((json.new_options_html)) {
		  $(json.new_options_html).each(function(i, val) {
			$('#new_poll_option_1').before(val.reply_html_1);
			$('#poll_option_box_1_' + val.poll_option_id).slideDown('slow');
			$('#new_poll_option_2').before(val.reply_html_2);
			$('#poll_option_box_2_' + val.poll_option_id).slideDown('slow');
		  });
		}
		if ((json.options_updates)) {
		  $(json.options_updates).each(function(i, val) {
			$('#option_bar_title_' + val.poll_option_id).attr('title', val.poll_option_percentage + '%');
			$('#option_bar_width_' + val.poll_option_id).attr('style', 'width : ' + val.poll_option_percentage + '%');
			$('#option_bar_count_' + val.poll_option_id).html(val.poll_option_reply_count + ' votes');
		  });
		}
	  }
	}
  });
  setTimeout('load_new_options_replies()', 60000);
}

function play_notification_sound() {
  var audio = new Audio();
  audio.setAttribute("src", site_url('assets/audio/inno_tweets_notify.wav'));
  audio.load();
  audio.play();
}

function navigate_photos() {
  document.onkeydown = function(evt) {
	evt = evt || window.event;
	switch (evt.keyCode) {
	  case 37:
		window.location.href = ($.trim($('#prev_photo_id').val()));
		break;
	  case 39:
		window.location.href = ($.trim($('#next_photo_id').val()));
		break;
	}
  };
  return;
}

function ignore_post(ignore_type, ignore_type_id) {
  $.ajax({
	type: 'POST',
	cache: false,
	url: site_url('ignore'),
	data: {
	  ignore_type: ignore_type,
	  ignore_type_id: ignore_type_id
	},
	success: function(data) {
	  if ($.trim(data) != '') {
		var json = $.parseJSON(data);
		if (json.data.total_ignore_count) {
		  if (json.data.report_status == 1) {
			toastr.success(json.data.total_ignore_count + ' user(s) reported this post !');
		  } else {
			toastr.error(json.data.total_ignore_count + ' user(s) reported this post !');
		  }
		  $('#ignore_' + ignore_type + '_' + ignore_type_id).attr('title', 'Ignore post | ' + json.data.total_ignore_count + ' ignores !')
		}
		if (json.data.ignore_status == 1) {
		  $('#' + ignore_type + '_' + ignore_type_id).slideUp('slow');
		  if (ignore_type == 'tweet') {
			location.reload();
		  }
		}
	  }
	}
  });
}

// Load more albums
function load_more_albums() {
  var album_type = $.trim($('#album_type').val());
  var album_offset = parseInt($.trim($('#album_offset').val()));
  $('#loading_animation').show();
  $.ajax({
	type: 'POST', cache: false,
	url: site_url('albums_load_more'), data: {
	  album_type: album_type,
	  album_offset: album_offset
	},
	success: function(data) {
	  $('#album_offset').val(album_offset + 12);
	  $('#loading_animation').hide();
	  if ($.trim(data) != '') {
		var json = $.parseJSON(data);
		if (json.data) {
		  $('#albums_more').before(json.data);
		}
		if (json.has_more == 0) {
		  $('#load_more_buttons').fadeOut();
		}
	  }
	}
  });
}

function set_title_count(count) {
  if (parseInt(count) && parseInt(count) > 0) {
	$('title').html('(' + parseInt(count) + ') ' + 'innoTweets');
  } else {
	$('title').html('innoTweets');
  }
}

function notification() {
  $.ajax({
	type: 'POST',
	cache: false,
	url: site_url('notification'),
	success: function(data) {
	  if ($.trim(data)) {
		var json = $.parseJSON(data);
		if (json.data) {
		  if (json.data.header_counts) {
			update_header_menu_counts(json.data.header_counts);
		  }
		  if (json.data.toast_notify) {
			show_notification_toaster(json.data.toast_notify);
		  }
		  if (json.data.unread_notify_count) {
			$('#user_notification_count').show();
			$('#user_notification_count').html(json.data.unread_notify_count);
			set_title_count(json.data.unread_notify_count);
		  } else {
			$('#user_notification_count').hide();
			set_title_count();
		  }
		}
	  }
	}
  });
  setTimeout('notification()', 60000); // 1 Minutes
}

function update_header_menu_counts(header_counts) {
  if (header_counts.tweets) {
	$('#tweets_total_count').show();
	$('#tweets_total_count').html(header_counts.tweets);
  } else {
	$('#tweets_total_count').hide();
  }
  if (header_counts.events) {
	$('#events_total_count').show();
	$('#events_total_count').html(header_counts.events);
  } else {
	$('#events_total_count').hide();
  }
  if (header_counts.albums) {
	$('#albums_total_count').show();
	$('#albums_total_count').html(header_counts.albums);
  } else {
	$('#albums_total_count').hide();
  }
  if (header_counts.polls) {
	$('#polls_total_count').show();
	$('#polls_total_count').html(header_counts.polls);
  } else {
	$('#polls_total_count').hide();
  }
  if (header_counts.groups) {
	$('#groups_total_count').show();
	$('#groups_total_count').html(header_counts.groups);
  } else {
	$('#groups_total_count').hide();
  }
  if (header_counts.online) {
	$('#online_total_count').show();
	$('#online_total_count').html(header_counts.online);
  } else {
	$('#online_total_count').hide();
  }
}

function show_notification_toaster(toast_notify) {
  $(toast_notify).each(function(i, val) {
	toastr.info(val);
	desktop_notification(val);
  });
}

function show_notification_list() {
  $('#notification_data').html('');
  $('#user_notification_count').hide();
  $('#notification_loading').show();
  set_title_count();
  $.ajax({
	type: 'POST',
	cache: false,
	url: site_url('notification/notify_list'),
	success: function(data) {
	  $('#notification_loading').hide();
	  if ($.trim(data)) {
		$('#notification_data').html(data);
	  }
	}
  });
}

function load_timeline(filter, offset, auto) {
  if (filter) {
	$('#timeline_filter').val(filter);
  } else {
	$('#timeline_filter').val('');
  }
  if (offset) {
	$('#timeline_offset').val(offset);
  } else {
	$('#timeline_offset').val(Math.round(new Date().getTime() / 1000));
  }
  if (filter || offset) {
	$('#timeline_body').html('');
	$('#timeline_more').html('');
  } else {
	$('#timeline_body').html('');
  }
  $('#loading_div_end').show();
  $('#timeline_filter_options').val(filter);
  $('#timeline_offset_options').val(offset);
  $('#timeline_filter_options').attr('disabled', 'true');
  $('#timeline_offset_options').attr('disabled', 'true');
  $.ajax({
	type: 'POST',
	cache: false,
	url: site_url('timeline/ajax'),
	data: {
	  offset: $('#timeline_offset').val(),
	  filter: $('#timeline_filter').val()
	},
	success: function(data) {
	  $('#loading_div_end').hide();
	  $('#timeline_body').html(data);
	  $('#timeline_filter_options').removeAttr('disabled', 'true');
	  $('#timeline_offset_options').removeAttr('disabled', 'true');
	}
  });
  if (auto == true && !offset && !filter) {
	//setTimeout('load_timeline(null, null, true)', 60000); // 1 Minute
  }
}

function load_timeline_more() {
  $(document).ready(function() {
	var tl_is_busy = false;
	$(window).scroll(function() {
	  if ($(window).scrollTop() + $(window).height() == $(document).height()) {
		if (tl_is_busy) {
		  return;
		}
		tl_is_busy = true;
		$('#loading_div_end').show();
		var curOffset = $('#timeline_offset').val();
		var curOffsetDateObj = new Date(curOffset * 1000);
		curOffsetDateObj.setDate(curOffsetDateObj.getDate() - 1);
		var newOffset = Math.round(curOffsetDateObj.getTime() / 1000);
		$.ajax({
		  async: false,
		  type: 'POST',
		  cache: false,
		  url: site_url('timeline/ajax'),
		  data: {
			offset: newOffset,
			filter: $('#timeline_filter').val()
		  },
		  success: function(data) {
			tl_is_busy = false;
			//$('#timeline_offset').val(newOffset);
			$('#loading_div_end').hide();
			$('#timeline_more').append(data);
		  },
		  error: function() {
			tl_is_busy = false;
			$('#loading_div_end').hide();
		  }
		});
	  }
	});
  });
}

function desktop_notification(text) {
  if ($.trim(text)) {
	if (window.webkitNotifications) {
	  if (window.webkitNotifications.checkPermission() === 0) {
		window.webkitNotifications.createNotification('', 'innoTweets', text).show();
	  } else {
		desktop_notification_permission(text);
	  }
	} else if (window.Notification) {
	  if (window.Notification.permission === 'granted') {
		new window.Notification('innoTweets', {'body': text});
	  } else {
		desktop_notification_permission(text);
	  }
	} else {
	  console.log("Web Notifications are not supported in this browser");
	}
  }
}

function desktop_notification_permission(text) {
  if (window.webkitNotifications) {
	window.webkitNotifications.requestPermission();
  } else if (window.Notification) {
	window.Notification.requestPermission(function() {
	  desktop_notification(text);
	});
  } else {
	console.log("Web Notifications are not supported in this browser");
  }
}

desktop_notification_permission(null);

// Show or Hide a Div
function show_hide(id) {
  if ($('#' + id).css('display') == 'none') {
	$('#' + id).slideDown();
  } else {
	$('#' + id).slideUp();
  }
}

function search_smiley_focus() {
  $("#smiley_search").live({
	focus: function(e) {

	},
	blur: function(e) {
	  this.value = '';
	  $("#smiley_list > li").show();
	},
	keyup: function() {
	  var value = $.trim(this.value);
	  if (value) {
		$("#smiley_list > li").hide();
		$("#smiley_list > li:contains('" + value + "')").show();
	  }
	}
  });
}

search_smiley_focus();

// Image zoom on hover
$(document).ready(function() {

  // Image zoom
  $('.img-zoom').hover(function() {
	$(this).addClass('img-zoom-transition');
  }, function() {
	$(this).removeClass('img-zoom-transition');
  });

  // Show tooltip
  $("a, img").on("mouseover", function() {
	$(this).tooltip();
  });
});